﻿Namespace FileSystemScan
    Friend Class Handler_GroupSubDirectories
        Friend Shared Sub Scan(ByVal share As Entities.Share, ByVal rootPath As String)
            Handler_ScanFolder.Scan(share, AddressOf GroupFilesToItemCandidates, AddressOf GetKeyForItem, rootPath)
        End Sub

        Private Shared Function GetKeyForItem(ByVal share As Entities.Share, ByVal item As Entities.SharedItem) As String
            Return item.Name.ToLower
        End Function

        Private Shared Function GroupFilesToItemCandidates(ByVal share As Entities.Share, ByVal files As Generic.List(Of IO.FileInfo)) As Generic.Dictionary(Of String, Handler_ScanFolder.ItemCandidate)
            Dim aRet As New Generic.Dictionary(Of String, Handler_ScanFolder.ItemCandidate)

            For Each clsFI As IO.FileInfo In files
                Dim strPath As String = clsFI.FullName

                Dim strSubPath As String = Nothing
                For Each strRoot As String In share.Paths
                    If Framework.IO.Utils.IsSubDir(strPath, strRoot) Then
                        strSubPath = strPath.Substring(strRoot.Length)
                        Exit For
                    End If
                Next
                Debug.Assert(strSubPath IsNot Nothing, "SubPath should never be nothing!")

                While strSubPath.StartsWith(IO.Path.DirectorySeparatorChar)
                    strSubPath = strSubPath.Substring(1)
                End While

                Dim aParts() As String = strSubPath.Split(IO.Path.DirectorySeparatorChar)
                If aParts.Length = 1 Then
                    'this is a file in the root - exclude it!
                Else
                    Dim clsCand As Handler_ScanFolder.ItemCandidate = GetItemCandidateNameByFilePath(aParts(0))
                    If aRet.ContainsKey(clsCand.Key) Then
                        clsCand = aRet(clsCand.Key)
                    Else
                        aRet.Add(clsCand.Key, clsCand)
                    End If
                    clsCand.AddFile(clsFI)
                End If
            Next
            Return aRet
        End Function

        Public Shared Function GetItemCandidateNameByFilePath(ByVal filePath As String) As Handler_ScanFolder.ItemCandidate
            Try
                Dim clsRet As New Handler_ScanFolder.ItemCandidate
                'remove everything but the actual filename
                Dim strFileName As String
                strFileName = System.IO.Path.GetFileName(filePath).Replace("."c, " "c)

                With Framework.IO.Utils.CleanUpFilename(strFileName)
                    strFileName = .Name
                    clsRet.Comment = .Comment
                End With

                clsRet.Name = strFileName
                clsRet.Key = strFileName.ToLower
                Return clsRet
            Catch ex As Exception
                Throw New Exception(String.Format("Failed parsing filePath ""{0}""!", filePath), ex)
            End Try
        End Function
    End Class
End Namespace
